package com.panshi.controller.asme;

import com.panshi.api.IAsmeApi;
import com.panshi.api.ILeaveApi;
import com.panshi.api.ILinksApi;
import com.panshi.api.SortApi;
import com.panshi.po.AsmePO;
import com.panshi.po.LeavePO;
import com.panshi.po.LinksPO;
import com.panshi.po.SortPO;
import com.panshi.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 关于我们表的控制层
 */
@Controller
@RequestMapping("/asme")
@Slf4j
public class AsmeController {

    //注入关于我们的接口
    @Autowired
    IAsmeApi iAsmeApi;

    //注入分类接口
    @Autowired
    SortApi sortApi;

    //注入用户留言
    @Autowired
    ILeaveApi iLeaveApi;

    //注入友情链接接口
    @Autowired
    ILinksApi iLinksApi;

    //关于我们
    @GetMapping("me/{id}")
    @ResponseBody
    public ModelAndView me(@PathVariable Integer id, HttpServletRequest request){
        gain(id,request);

        PageVO pageVO = new PageVO();
        pageVO.setCurrenPage(1);
        pageVO.setSize(10);
        sortQuery(pageVO,request);

        ModelAndView mv = new ModelAndView("website/me");

        acme(request);
        links(request);

        return mv;
    }

    //联系我们
    @GetMapping("link/{id}")
    @ResponseBody
    public ModelAndView link(@PathVariable Integer id,HttpServletRequest request){
        gain(id,request);
        PageVO pageVO = new PageVO();
        pageVO.setCurrenPage(1);
        pageVO.setSize(10);
        sortQuery(pageVO,request);
        ModelAndView mv = new ModelAndView("website/link");
        acme(request);
        links(request);
        return mv;
    }


    //网站地图
    @GetMapping("map/{id}")
    @ResponseBody
    public ModelAndView map(@PathVariable Integer id,HttpServletRequest request){
        ModelAndView mv = new ModelAndView("website/map");
        gain(id,request);


        PageVO pageVO = new PageVO();
        pageVO.setCurrenPage(1);
        pageVO.setSize(10);
        sortQuery(pageVO,request);

        acme(request);
        links(request);
        return mv;
    }




    //招聘信息
    @GetMapping("recruit/{id}")
    @ResponseBody
    public ModelAndView recruit(@PathVariable Integer id,HttpServletRequest request){
        gain(id,request);
        PageVO pageVO = new PageVO();
        pageVO.setCurrenPage(1);
        pageVO.setSize(10);
        sortQuery(pageVO,request);
        ModelAndView mv = new ModelAndView("website/recruit");
        acme(request);
        links(request);
        return mv;
    }

    //用户协议
    @GetMapping("agreement/{id}")
    @ResponseBody
    public ModelAndView agreement(@PathVariable Integer id,HttpServletRequest request){
        gain(id,request);
        PageVO pageVO = new PageVO();
        pageVO.setCurrenPage(1);
        pageVO.setSize(10);
        sortQuery(pageVO,request);
        ModelAndView mv = new ModelAndView("website/agreement");
        acme(request);
        links(request);
        return mv;
    }

    //留言反馈--渲染数据
    @GetMapping("leave/{id}")
    @ResponseBody
    public ModelAndView leave(@PathVariable Integer id,HttpServletRequest request){

        gain(id,request);

        ModelAndView mv = new ModelAndView("website/leave");

        PageVO pageVO=new PageVO ();
        pageVO.setCurrenPage(1);
        pageVO.setSize(10);
        sortQuery(pageVO,request);

        acme(request);

        links(request);

        return mv;
    }

    //留言反馈---提交
    @PostMapping("submit")
    @ResponseBody
    public ResponseVO addLeave(@RequestBody AddLeaveVO addLeaveVO, HttpServletRequest request){

        String code = (String) request.getSession().getAttribute("code");

        if(!code.equals(addLeaveVO.getCode())){
           return new ResponseVO(false,"验证码错误","500",code);
        }

        request.getSession().removeAttribute("code");

        log.info("code===={}",code);

        addLeaveVO.setCreatedate(new Date());

        Integer integer=iLeaveApi.addLeavePostBody(addLeaveVO).getData();

        PageVO pageVO=new PageVO ();
        pageVO.setCurrenPage(1);
        pageVO.setSize(10);
        sortQuery(pageVO,request);

        acme(request);

        links(request);

        return new ResponseVO(true, "尊敬的"+addLeaveVO.getAttn()+",感谢您的留言，我们将第一时间反馈给您","200",integer);

    }

    //分类
    @PostMapping("sort")
    public ModelAndView sortQuery(PageVO pageVO, HttpServletRequest request){

        ModelAndView mv = new ModelAndView("comment/middle");

        ResponseVO responseVO=sortApi.sortQuery(pageVO);

        List<SortPO> list= (List<SortPO>) responseVO.getData();

        request.setAttribute("SortPO",list);

        return mv;

    }


    public void gain(Integer id,HttpServletRequest request){
        PageBeanVo pageBeanVo = new PageBeanVo();
        pageBeanVo.setCurrenPage(1);
        pageBeanVo.setPageSize(10);
        List<AsmePO> lists=iAsmeApi.queryAsmePostBody(pageBeanVo).getData();

        QueryIdVO  queryIdVO = new QueryIdVO();
        queryIdVO.setId(id);
        ResponseVO<AsmePO> responseVO= iAsmeApi.queryIdAsmePostBody(queryIdVO);
        AsmePO asmePO=responseVO.getData();

        log.info("asmePO============{}",asmePO);

        request.setAttribute("mePo",asmePO);
        request.setAttribute("allPo",lists);
    }

    //问题首页加载关于我们
    @PostMapping("as")
    public ModelAndView acme(HttpServletRequest request){
        ModelAndView mv = new ModelAndView("comment/button");
        PageBeanVo pageBeanVo =new PageBeanVo();
        pageBeanVo.setCurrenPage(1);
        pageBeanVo.setPageSize(10);

        //获取关于我们的列表
        List<AsmePO> list= iAsmeApi.queryAsmePostBody(pageBeanVo).getData();

        //存放每个对象的容器
        List<AsmePO>  linksAndMeVoList = new ArrayList<>();

        log.info("获取关于我们的列表==============================={}",list);

        for (int i=0; i<list.size(); i++){
            linksAndMeVoList.add(list.get(i));
        }
        request.setAttribute("meVo",list);
        return mv;
    }

    //问题首页加载友情链接
    @PostMapping("links")
    public ModelAndView links(HttpServletRequest request){
        ModelAndView mv = new ModelAndView("comment/button");
        PageBeanVo pageBeanVo =new PageBeanVo();
        pageBeanVo.setCurrenPage(1);
        pageBeanVo.setPageSize(10);

        //获取关于我们的列表
        List<LinksPO> list= iLinksApi.queryLinksPostBody(pageBeanVo).getData();

        log.info("queryLinksPostBody======{}",iLinksApi.queryLinksPostBody(pageBeanVo).getData());

        //存放每个对象的容器
        List<LinksPO>  linksAndMeVoList = new ArrayList<>();

        for (int i=0; i<list.size(); i++){
            linksAndMeVoList.add(list.get(i));
        }
        request.setAttribute("linksVo",list);
        return mv;
    }

}
